
package character_18;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class Character_18 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        String[] strs = {"98", "2", "3", "4"};
        for (String string : sorted(strs)) {
            System.out.println(string);
        }
        
        Node<Apple> apple = new Node<>(new Apple(), null);
        //Node<Fruit> fruit = apple;//编译不通过
        Node<? extends Fruit> fNode = apple;//编译可通过
        
        Node<Apple> apple1 = new Node<>(new Apple(), null);
        Node<Apple> apple2 = new Node<>(new Apple(), apple1);
        Node<Apple> apple3 = new Node<>(new Apple(), apple2);
        
        Node<Banana> banan1 = new Node<>(new Banana(), null);
        Node<Banana> banan2 = new Node<>(new Banana(), banan1);
        
        show(apple3);
        show(banan2);
        
        fNode.value = null;
        //Apple a = fNode.value;//编译均出错
        //fNode.value = new Apple();
        
        List<Integer> l1 = new ArrayList<Integer>();
        List<String> l2 = new ArrayList<String>();
        System.out.println(l1.equals(l2));//因为l1与l2都为空，所以视为一样的
        
        Basket<Integer> b1 = new Basket<Integer>(1,2);
        Basket<String> b2 = new Basket<String>("1", "2");
        System.out.println(b1.equals(b2));//考虑了继承关系和hashCode
        
        Comparator<Fruit> pC = (f1, f2) -> f1.price - f2.price;
        Basket<Apple> app = new Basket<>(new Apple(25, 125), new Apple(20, 100));
        app.sort(pC);
        for (Apple s : app.things) {
            System.out.println(s.price + "||" + s.weight);
        }
        System.out.println();
        
        Basket<Banana> bananas = new Basket<>(new Banana(50, 300),new Banana(30, 200));
        bananas.sort(pC);
        for (Banana banana : bananas.things) {
            System.out.println(banana.price + "||" + banana.weight);
        }
        System.out.println();
        
        new Extends().test();
    }
    
    //@SelfDefine(timeout = 10)
    public static void show(Node<? extends Fruit> n){
    
        Node<? extends Fruit> node = n;
        do {            
            System.out.println(node.value);
            node = node.next;
        } while (node != null);
    } 
    
    void test(){
    
        Duck<Animal> ad = new Duck<Animal>();
        Duck<Human> hd = new Duck<Human>();
        
    }
    
    public static <T extends Comparable<T>> T[] sorted(T[] array){
    
        T[] arr = Arrays.copyOf(array, array.length);
        sort(arr, 0, arr.length - 1);
        return arr;
    }
    
    private static void sort(Object[] array, int left, int right){
    
        if (left < right) {
            int q = partition(array, left, right);
            sort(array, left, q-1);
            sort(array, q+1, right);
        }
    }
    
    private static int partition(Object[] array, int left, int right){
    
        int i = left -1;
        for (int j = left; j < right; j++) {
            if (((Comparable)array[j]).compareTo(array[right]) <= 0) {
                i++;
                swap(array, i, j);
            }
        }
        swap(array, i + 1, right);
        return i + 1;
    }
    
    private static void swap(Object[] array, int i, int j){
    
        Object t = array[i];
        array[i] = array[j];
        array[j] = t;
    }
    
public class Animal{}
public class Human extends Animal{}
public class Duck<T extends Animal>{}
}
